﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using Lenovo.CFI.BLL.Sys;
using Lenovo.CFI.Common;

namespace Lenovo.CFI.Web.Helper
{
    /// <summary>
    /// 绑定帮助类
    /// </summary>
    public class BindHelper
    {
        public static void SetSelectItem(ListControl ddl, string selectedValue)
        {
            if (ddl.SelectedItem != null)
                ddl.SelectedItem.Selected = false;
            ListItem item = ddl.Items.FindByValue(selectedValue);
            if (item != null)
                item.Selected = true;
        }

        private static void SetDefaultItem(ListControl ddl, string defaultText, string defaultValue, string selectedValue)
        {
            if (!String.IsNullOrEmpty(defaultText))
            {
                ddl.Items.Insert(0, new ListItem(defaultText, defaultValue));
            }

            if (!String.IsNullOrEmpty(selectedValue))
            {
                ListItem item = ddl.Items.FindByValue(selectedValue);
                if (item != null)
                    item.Selected = true;
            }
        }

        public static void BindIdeaType(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = BLL.DicMgr.IdeaTypeDicMgr.Get(withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindNextAction(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = BLL.DicMgr.NextActionDicMgr.Get(withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindOtherBu(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = BLL.DicMgr.OtherBuDicMgr.Get(withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindKeyword(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = BLL.DicMgr.KeywordDicMgr.Get(withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindTopicType(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = BLL.DicMgr.TopicTypeDicMgr.Get(withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }





        public static void BindTopics(ListControl ddl, string defaultText, string defaultValue, string selectedValue)
        {
            ddl.Items.Clear();

            ddl.DataSource = new BLL.TopicBl().GetTopic();
            ddl.DataTextField = "Title";
            ddl.DataValueField = "ID";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }







        public static void BindUser(ListControl ddl, string defaultText, string defaultValue, string selectedValue)
        {
            ddl.Items.Clear();

            ddl.DataSource = new BLL.Sys.UserBl().GetUserAll();
            ddl.DataTextField = "DisplayID";
            ddl.DataValueField = "ItCode";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindUser(string defaultText, string defaultValue, string selectedValue, params ListControl[] ddls)
        {
            List<Common.Sys.User> users = new BLL.Sys.UserBl().GetUserAll();
            foreach (ListControl ddl in ddls)
            {
                ddl.Items.Clear();

                ddl.DataSource = users;
                ddl.DataTextField = "DisplayID";
                ddl.DataValueField = "ItCode";
                ddl.DataBind();

                SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
            }
        }

        public static void BindUser(ListControl ddl, string defaultText, string defaultValue, string selectedValue, int teamID)
        {
            ddl.Items.Clear();

            ddl.DataSource = new BLL.Sys.UserBl().GetUser(null, teamID, null, null, true, null);
            ddl.DataTextField = "DisplayID";
            ddl.DataValueField = "ItCode";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindTeam(ListControl ddl, string defaultText, string defaultValue, string selectedValue)
        {
            ddl.Items.Clear();

            ddl.DataSource = new BLL.Sys.TeamBl().GetTeam(null, null, null, null);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "ID";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindTeam(ListControl ddl, string defaultText, string defaultValue, string selectedValue, List<Common.Sys.Team> teams)
        {
            ddl.Items.Clear();

            ddl.DataSource = teams;
            ddl.DataTextField = "Title";
            ddl.DataValueField = "ID";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindTeam(string defaultText, string defaultValue, string selectedValue, params ListControl[] ddls)
        {
            List<Common.Sys.Team> teams = new BLL.Sys.TeamBl().GetTeam(null, null, null, null);
            foreach (ListControl ddl in ddls)
            {
                ddl.Items.Clear();

                ddl.DataSource = teams;
                ddl.DataTextField = "Title";
                ddl.DataValueField = "ID";
                ddl.DataBind();

                SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
            }
        }

        public static void BindAllUsers(ListControl ddl, string defaultText, string defaultValue, string selectedValue)
        {
            ddl.Items.Clear();

            ddl.DataSource = new UserBl().GetUserAll();
            ddl.DataTextField = "RealName";
            ddl.DataValueField = "ItCode";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }
    }
}